#导包
import unittest
import requests
from IHRM.api.employee import emploteeAPI
from IHRM.api.login import loginAPI
from IHRM import app
from IHRM.utils import com_assert


class Testemployee(unittest.TestCase):
    # 存放员工id
    yid = None

    #前置处理
    def setUp(self):
        self.employee_api = emploteeAPI()
        self.login_api = loginAPI()
        port= self.login_api.login({"mobile": "13800000002", "password": "929itheima.CN032@.20250212"})
        print(port.json())

        # 提取token信息
        app.TOKEN = "Bearer " + port.json().get("data")
        print(app.TOKEN)
        # 把提取的token信息返回给app文件下的TOKEN
        app.headers_data["Authorization"] = app.TOKEN
        print(app.headers_data["Authorization"])

    #添加员工测试用例设计
    def test01(self):
        add_data = {
            "username": "mikey1",  #用户名唯一
            "mobile": "1153386303", #手机号唯一
            "timeOFEntry": "2025-02-11",
            "fromOFEnployment": 1,
            "workNumber": "10088",  #员工id唯一
            "departmentName": "销售",
            "departmantId": "126669905768001024",
            "correctionTime": "2025-01-09T17:00:00.002"
        }

        #获取响应结果
        response = self.employee_api.employee01(add_data = add_data)
        print(response.json())

        #断言
        com_assert(self, response, 200, True, 10000, "操作成功")

    #获取员工id
    def test02(self):
        #查询员工信息
        global yid
        ids = self.employee_api.id()
        #查看员工信息
        print(ids.json())

        #断言
        self.assertEqual(200, ids.status_code)

        #查找路径
        liu = ids.json().get("data", {}).get("rows", [])
        #遍历返回员工信息
        for ess in liu:
            #查询符合的员工id
            if ess.get("username") == "mikey1":
                yid = ess.get("id")
                break
        #找到就打印查找的员工id
        if yid:
            print(yid)
        #否则就打印未找到
        else:
            print("未找到")
    #修改员工信息
    def test03(self):
        #修改的内容
        update_data = {
            "username": "mikey0"  #用户名唯一
        }

        #获取响应结果
        response = self.employee_api.employee02(yid, update_data)
        print(response.json())

        #断言
        com_assert(self, response, 200, True, 10000, "操作成功")

    #员工查询
    def test04(self):
        #获取响应结果
        response = self.employee_api.employee03(yid)
        print(response.json())

        #断言
        com_assert(self, response, 200, True, 10000, "操作成功")

    def test05(self):
        # 获取响应结果
        response = self.employee_api.employee04(yid)
        print(response.json())

        # 断言
        com_assert(self, response, 200, True, 10000, "操作成功")
